package com.teangtang.fly.controller;

import com.teangtang.fly.dto.*;
import com.teangtang.fly.entity.Airline;
import com.teangtang.fly.entity.City;
import com.teangtang.fly.entity.Province;
import com.teangtang.fly.service.AirlineService;
import com.teangtang.fly.service.CityService;
import com.teangtang.fly.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/airline")
public class AirlineController {

    @Autowired
    private AirlineService airlineService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @GetMapping
    public List<AirlineDto> getAllAirlines() {
        List<Airline> airlines = airlineService.getAllAirlines();
        List<AirlineDto> airlineDtos = new ArrayList<>();
        for (Airline airline : airlines) {
            City departureCity = airline.getDepartureCity();
            City arrivalCity = airline.getArrivalCity();
            AirlineDto airlineDto = new AirlineDto();
            airlineDto.setAirlineNumber(airline.getAirlineNumber());
            airlineDto.setDepartureCityName(departureCity.getCityName());
            airlineDto.setDepartureAirportName(airline.getDepartureAirportName());
            airlineDto.setArrivalCityName(arrivalCity.getCityName());
            airlineDto.setArrivalAirportName(airline.getArrivalAirportName());
            airlineDto.setDistance(airline.getDistance());
            airlineDtos.add(airlineDto);
        }
        return airlineDtos;
    }

    @PostMapping("/flights")
    public ResponseEntity<List<FlightDto>> getAirlinesByProvinceCode(@RequestBody FindFlightDTO findFlightDTO) {
        List<FlightDto> flightDto = airlineService.getFlightByCityAndTime(findFlightDTO);;
        return new ResponseEntity<>(flightDto, HttpStatus.OK);
    }

    @GetMapping("/{airlineNumber}")
    public Airline getAirlineByAirlineNumber(@PathVariable String airlineNumber) {
        return airlineService.getAirlineByAirlineNumber(airlineNumber);
    }

    @PostMapping
    public ResponseEntity<AirlineDto> createCity(@RequestBody AirlineDto airlineDto) {

        City departureCity = cityService.findCityByCityCode(airlineDto.getDepartureCityCode());
        City arrivalCity = cityService.findCityByCityCode(airlineDto.getArrivalCityCode());
        Airline airline = new Airline();
        airline.setAirlineNumber(airlineDto.getDepartureAirportName()+"-"+airlineDto.getArrivalAirportName());
        airlineDto.setAirlineNumber(airline.getAirlineNumber());
        airline.setDepartureCity(departureCity);
        airline.setArrivalCity(arrivalCity);
        airline.setDepartureAirportName(airlineDto.getDepartureAirportName());
        airline.setArrivalAirportName(airlineDto.getArrivalAirportName());
        airline.setDistance(airlineDto.getDistance());
        List<Airline> departureAirlines = departureCity.getDepartureAirlines();
        departureAirlines.add(airline);
        departureCity.setDepartureAirlines(departureAirlines);
        List<Airline> arrivalAirlines = arrivalCity.getArrivalAirlines();
        arrivalAirlines.add(airline);
        arrivalCity.setArrivalAirlines(arrivalAirlines);
        airlineService.createAirline(airline);
        cityService.saveCity(departureCity);
        cityService.saveCity(arrivalCity);
        return new ResponseEntity<>(airlineDto, HttpStatus.CREATED);
    }


    @PutMapping("/{airlineNumber}")
    public Airline updateAirline(@PathVariable String airlineNumber, @RequestBody Airline airline) {
        return airlineService.updateAirline(airlineNumber, airline);
    }

    @DeleteMapping("/{airlineNumber}")
    public void deleteairline(@PathVariable String airlineNumber) {
        airlineService.deleteAirline(airlineNumber);
    }
}
